<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/7/12
 * Time: 14:16
 */
namespace app\admin\controller;

use think\Db;
use think\Request;

class GoodsController extends BaseController{
//产品列表
  public function  lst(){
          $start1 = Request::instance()->param('start');
          $end1 = Request::instance()->param('end');
          $status1 = Request::instance()->param('status');
          $keyword1 = Request::instance()->param('keyword');

      if($start1 || $end1 || $status1 !='' || $keyword1){
    //接收搜索条件
       $start = Request::instance()->param('start') ? strtotime(Request::instance()->param('start')) : 0;
       $end = Request::instance()->param('end') ? strtotime(Request::instance()->param('end')) : time();
       $sql = "g.created_time > $start and g.created_time < $end";
       $status = Request::instance()->param('status')=='' ?'' : Request::instance()->param('status');

       if($status != ''){
           $sql.=" and g.status=$status";
       }
       $keyword = Request::instance()->param('keyword') ? Request::instance()->param('keyword') : '';
       if($keyword){
           $sql.=" and (g.goods_name like '%$keyword%' or g.assessment_name like '%$keyword%')";
       }

       $goods = Db::table('goods')
           ->alias('g')
           ->join('goods_type gt','g.type_id=gt.id')
           ->join('assessment_cycle ac','g.cycle_id=ac.id')
           ->field('g.*,gt.name goods_type_name,ac.name assessment_cycle_name')
           ->where($sql)
           ->paginate(2,false,$config=['query'=>['start'=>$start1,'end'=>$end1,'status'=>$status1,'keyword'=>$keyword1]]);

   }else{

       $goods = Db::table('goods')
           ->alias('g')
           ->join('goods_type gt','g.type_id=gt.id')
           ->join('assessment_cycle ac','g.cycle_id=ac.id')
           ->field('g.*,gt.name goods_type_name,ac.name assessment_cycle_name')
           ->paginate(2);
   }

      $start = Request::instance()->param('start');
      $end = Request::instance()->param('end');
      $status = Request::instance()->param('status');
      $keyword = Request::instance()->param('keyword');

      $this->assign('start',$start);
      $this->assign('end',$end);
      $this->assign('status',$status);
      $this->assign('keyword',$keyword);
      $this->assign('goods',$goods);
      return $this->fetch();
 }
//添加产品
public function add(){
    if(Request::instance()->isPost()){
        $data = [
            'type_id'=>Request::instance()->param('type_id'),
            'goods_name'=>Request::instance()->param('goods_name'),
            'assessment_name'=>Request::instance()->param('assessment_name'),
            'goods_life'=>Request::instance()->param('goods_life'),
            'cycle_id'=>Request::instance()->param('cycle_id'),
            'window_period'=>Request::instance()->param('window_period'),
            'status'=>Request::instance()->param('status'),
            'price'=>Request::instance()->param('price'),
        ];

        //验证数据
        $validate = validate('Goods');
        if($validate->check($data)){
            //验证成功判断,根据名称查找，如果已存在则不添加
            $good = Db::table('goods')->where('goods_name',$data['goods_name'])->find();
            if($good){
                $this->error('产品已存在');
            }else{
                //图片上传获取图片路径
                if (!empty($_FILES['file']['tmp_name'])) {
                    $path ='uploads/admin/';
                    $thumbPrefix = 'thumb_';
                    $original_img = $path.date('YmdHis') . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                    $thumb_img = $path . $thumbPrefix.date('YmdHis') . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                    $re = move_uploaded_file($_FILES['file']['tmp_name'], $original_img);
                    if($re){
                        //上传成功，制作缩略图
                        $image = \think\Image::open($original_img);
                        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                        $image->thumb(50, 50)->save($thumb_img);
                    }
                    //move_uploaded_file($_FILES['ads_img']['tmp_name'], $thumb_img);
                    $data['goods_img'] = $original_img;
                    $data['goods_img_thumb'] = $thumb_img;

                }
                //去添加
                $data['created_time'] = time();
                $data['updated_time'] = time();
                $re = Db::table('goods')->insert($data);
                if($re){
                    $this->success('添加成功',url('goods/lst'));
                }else{
                    $this->error('添加失败');
                }
            }
        }else{
            $error = $validate->getError();
            $this->error($error);
        }


    }
    $types = Db::table('goods_type')->where('is_del != 1')->select();
    $cycles = Db::table('assessment_cycle')->where('is_del!=1')->select();
    $this->assign('types',$types);
    $this->assign('cycles',$cycles);
    return $this->fetch();
}
//编辑产品
public function edit(){
     //根据产品ID修改产品
    $id = Request::instance()->param('id');
    if(Request::instance()->isPost()){

    }
    $goods = Db::table('goods')->where('id',$id)->find();
    $this->assign('goods',$goods);
    return $this->fetch();


}
//查看产品
public function view(){
//根据id查看产品详细信息
    $id = Request::instance()->param('id');
    $good = Db::table('goods')
        ->alias('g')
        ->join('goods_type gt','g.type_id=gt.id')
        ->join('assessment_cycle ac','g.cycle_id=ac.id')
        ->field('g.*,gt.name goods_type_name,ac.name assessment_cycle_name')
        ->where('g.id',$id)
        ->find();
    $this->assign('good',$good);
    return $this->fetch();


}
//删除产品
public function delete(){
 //如果关于此产品有订单则不给删除，
    $id = Request::instance()->param('id');
    $goods_order = Db::table('order_details')
                   ->alias('od')
//                   ->join('order o','od.order_num = o.order_num')
                   ->where('od.goods_id',$id)
//                   ->where('o.status',1)
                   ->select();
    if($goods_order){
        $this->error('产品已销售，不允许删除',url('goods/lst'));
    }else{
        //删除产品
        $re = Db::table('goods')->where('id',$id)->delete();
        if($re){
           $this->success('删除成功',url('goods/lst'));
        }else{
            $this->error('删除失败',url('goods/lst'));
        }
    }
}
//更改产品的状态
public function  change_status(){
    $status = Request::instance()->param('status');
    $id = Request::instance()->param('id');
    $data = [
        'status'=>$status,
        'updated_time'=>time(),
    ];
    $re = Db::table('goods')->where('id',$id)->update($data);
    if($re){
        $this->success('更改成功',url('goods/lst'));
    }else{
        $this->error('更改失败',url('goods/lst'));
    }

}
    //

}